package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.components.element.VectorElement;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;
public class SnackbarSlice extends AbilitySlice {

    private Image back_button, swipe_image, tap_image, floating_image, infin_image,
            push_image, from_image, face_image;
    private Text text_title;
    private Button buttons;
    private boolean isShowOrNot = false;
    private boolean isShowOrNot_push = false;
    private boolean isShowOrNot_from = false;
    private boolean isShowOrNot_infinite = false;
    private DirectionalLayout swipe_lin, tap_lin, float_lin, inifin_lin, push_lin, from_lin, dismiss_lin, dismiss_top_lin, dismiss_infinite_lin;
    private AnimatorProperty animator;
    private DependentLayout face_lin;
    private AnimatorProperty property;
    private AnimatorProperty topLinAnimatorProperty;
    private AnimatorProperty infiniteLinAnimatorProperty;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_snackbar);
        initCheckbox();
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Snackbar")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Snackbar")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Snackbar", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Snackbar", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Snackbar", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        isShowOrNot_push = true;
        isShowOrNot_from = true;
        isShowOrNot_infinite = true;
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        face_image = (Image) findComponentById(ResourceTable.Id_face_image);
        dismiss_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_dismiss_lin);
        dismiss_top_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_dismiss_top_lin);
        dismiss_infinite_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_dismiss_infinite_lin);
        VectorElement element = new VectorElement(getContext(), ResourceTable.Graphic_ic_face_24px);
        face_image.setImageElement(element);
        buttons = (Button) findComponentById(ResourceTable.Id_snackbar_btn);
        face_lin = (DependentLayout) findComponentById(ResourceTable.Id_face_lin);
        topLinAnimatorProperty = dismiss_top_lin.createAnimatorProperty();
        infiniteLinAnimatorProperty = dismiss_infinite_lin.createAnimatorProperty();
        animator = dismiss_lin.createAnimatorProperty();
        property = face_lin.createAnimatorProperty();
        animator.moveFromY(800).moveToY(800).alpha(0);
        topLinAnimatorProperty.moveFromY(800).moveToY(800).alpha(0);
        infiniteLinAnimatorProperty.moveFromY(800).moveToY(800).alpha(0);
        buttons.setClickedListener(component -> {
            animator.reset();
            dismiss_lin.setVisibility(Component.VISIBLE);
            animator.moveFromY(500).moveToY(400).alpha(1).setDuration(300).start();
            new EventHandler(EventRunner.getMainEventRunner()).postTask(() -> {
                animator.moveFromY(800).moveToY(800).alpha(0);
                animator.reset();
                animator.moveFromY(500).moveToY(400).alpha(0).setDuration(1000);
                animator.start();
            }, 2000);
            if (!isShowOrNot_push) {
                property.moveFromY(300).moveToY(200).alpha(1).setDuration(300).start();
                new EventHandler(EventRunner.getMainEventRunner()).postTask(() -> {
                    property.moveFromY(200).moveToY(300).alpha(1).setDuration(300);
                    property.start();
                }, 2000);

            }else {
                if (!isShowOrNot_from) {
                    dismiss_lin.setVisibility(Component.HIDE);
                    animator.stop();
                    property.stop();
                    dismiss_top_lin.setVisibility(Component.VISIBLE);
                    new EventHandler(EventRunner.getMainEventRunner()).postTask(() -> {
                        dismiss_top_lin.setVisibility(Component.HIDE);
                    }, 2000);
                }

                if (!isShowOrNot_infinite) {
                    dismiss_lin.setVisibility(Component.HIDE);
                    animator.stop();
                    property.stop();
                    dismiss_infinite_lin.setVisibility(Component.VISIBLE);
                    new EventHandler(EventRunner.getMainEventRunner()).postTask(() -> {
                        dismiss_infinite_lin.setVisibility(Component.HIDE);
                    }, 2000);
                }
            }

        });
        text_title.setText("Snackbar");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    protected void onBackground() {
        super.onBackground();
//        dismiss_lin.setVisibility(Component.INVISIBLE);
    }

    private void initCheckbox() {
        swipe_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_swipe_lin);
        tap_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_tap_lin);
        float_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_floating_lin);
        inifin_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_infin_lin);
        push_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_push_lin);
        from_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_from_lin);
        swipe_image = (Image) findComponentById(ResourceTable.Id_swipe_checkbox);
        tap_image = (Image) findComponentById(ResourceTable.Id_tap_checkbox);
        floating_image = (Image) findComponentById(ResourceTable.Id_floating_checkbox);
        infin_image = (Image) findComponentById(ResourceTable.Id_infin_checkbox);
        push_image = (Image) findComponentById(ResourceTable.Id_push_checkbox);
        from_image = (Image) findComponentById(ResourceTable.Id_from_checkbox);
        swipe_lin.setClickedListener(component -> {
            if (isShowOrNot) {
                swipe_image.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                isShowOrNot = false;
                isShowOrNot_push = true;
                isShowOrNot_infinite = true;
                isShowOrNot_from = true;
            } else {
                swipe_image.setPixelMap(ResourceTable.Media_checkbox_false);
                isShowOrNot = true;
            }
        });
        tap_lin.setClickedListener(component -> {
            if (isShowOrNot) {
                tap_image.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                isShowOrNot = false;
                isShowOrNot_push = true;
                isShowOrNot_infinite = true;
                isShowOrNot_from = true;
            } else {
                tap_image.setPixelMap(ResourceTable.Media_checkbox_false);
                isShowOrNot = true;
            }

        });
        inifin_lin.setClickedListener(component -> {
            if (isShowOrNot) {
                infin_image.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                isShowOrNot = false;
                isShowOrNot_push = true;
                isShowOrNot_infinite = true;
                isShowOrNot_from = true;
            } else {
                infin_image.setPixelMap(ResourceTable.Media_checkbox_false);
                isShowOrNot = true;
            }
        });
        float_lin.setClickedListener(component -> {
            if (isShowOrNot) {
                floating_image.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                isShowOrNot = false;
                isShowOrNot_infinite = false;
                isShowOrNot_push = true;
                isShowOrNot_from = true;
            } else {
                floating_image.setPixelMap(ResourceTable.Media_checkbox_false);
                isShowOrNot = true;
                isShowOrNot_infinite = true;
            }
        });
        push_lin.setClickedListener(component -> {
            if (isShowOrNot_push) {
                push_image.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                isShowOrNot_push = false;
                isShowOrNot_infinite = true;
                isShowOrNot_from = true;
            } else {
                push_image.setPixelMap(ResourceTable.Media_checkbox_false);
                isShowOrNot_push = true;
            }
        });
        from_lin.setClickedListener(component -> {
            if (isShowOrNot_from) {
                from_image.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                isShowOrNot_from = false;
                isShowOrNot_push = true;
                isShowOrNot_infinite = true;
                isShowOrNot = true;
            } else {
                from_image.setPixelMap(ResourceTable.Media_checkbox_false);
                isShowOrNot_from = true;
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
